import {
  OrderModel
} from '../../models/orderData.js'
let orderModel = new OrderModel()
var sliderWidth = 103;

Page({
  data: {
    tabs: ["进行单", "待收款", "已完成"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    listData1: [],
    listData2: [],
    listData3: [],
  },
  onLoad: function() {
    wx.setNavigationBarTitle({
      title: '我的订单'
    })
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#3B80FE',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
    this.requestData('0')
  },

  requestData(index) {
    wx.showLoading({
      mask: true
    })
    var schemaIdTmp
    if (index == '0') {
      schemaIdTmp = '922e0618-eb6f-4ba7-836b-2b7c89934952'
    } else if (index == '1') {
      schemaIdTmp = '99e93718-d15c-424d-8b8a-453da246abb2'
    } else {
      schemaIdTmp = 'ed07580d-a5d6-4110-a97e-10d824a820ca'
    }
    let data = orderModel.getOrderList({
      listId: '285f7202-2b0a-42b1-ab1b-9302f53917db',
      unitId: 'c91854d1-b301-49fb-aba1-7cac8e161e8e',
      pageCode: 'ATC.Order.List',
      schemaId: schemaIdTmp,
    })
    data.then(res => {
      if (index == '0') {
        this.setData({
          listData1: res.data.data.data.records
        })
      } else if (index == '1') {
        this.setData({
          listData2: res.data.data.data.records
        })
      } else {
        this.setData({
          listData3: res.data.data.data.records
        })
      }
      wx.hideLoading()
    })
  },

  tabClick: function(e) {
    if (this.data.activeIndex == e.currentTarget.id) {
      return
    }
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
    this.requestData(e.currentTarget.id)
  },

  onShow: function () {
    this.requestData('0')
  },

  clickOrder(event) {
    wx.navigateTo({
      url: `/pages/orderDetail/orderDetail?FId=${event.detail.orderData.FId}&companyId=${event.detail.orderData.FCompany}`,
    })
  },
});